//
//  FirestViewController.m
//  Boom
//
//  Created by 云鹏 赵 on 15/3/5.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "FirestViewController.h"

@interface FirestViewController ()

@end

@implementation FirestViewController

- (void)dealloc
{
    [_myScrollView release];
    [_dataArray1 release];
    [_dataArray2 release];
    [_dataDic2 release];
    [_dataArray2DT release];
    [_dataArray2QG release];
    [_dataArray2YY release];
    [_dataArray2ZX release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 本试图控制器用于获取接口数据 并显示列表视图
    
    // 添加scrollView
    self.myScrollView = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.myScrollView.myDelegate = self;
    self.myScrollView.imageViewDelegate = self;
    [self.view addSubview:self.myScrollView];
    [_myScrollView release];
    self.myScrollView.multipleTouchEnabled = YES;
    
    // AFN获取接口数据
    [self afnGetData];

    // 下拉刷新
    [self addHeader];
}


// 下拉刷新
- (void)addHeader
{
    __block FirestViewController *fvc = self;
    [self.myScrollView addHeaderWithCallback:^{
        
        [fvc afnGetData];
        
        //（为了模拟延迟加载数据，因此2秒后才让刷新请求结束，实际项目中可以在数据请求成功后，停止下拉效果）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 结束刷新
            [fvc.myScrollView headerEndRefreshing];
        });
    }];
    // 自动刷新
    [self.myScrollView headerBeginRefreshing];
}



// afn获取数据
- (void)afnGetData
{
    // AFN获取接口数据
    // 获取第一部分数据
    self.dataArray1 = [NSMutableArray array];
    self.dataArray2 = [NSMutableArray array];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/super_explore_index?device=iPhone&includeActivity=true&picVersion=5&scale=2"];
    url_string = [url_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        // 刷新清楚原数据
        [self.dataArray1 removeAllObjects];
        [self.dataArray2 removeAllObjects];
//        NSLog(@"%@",responseObject);
        NSMutableDictionary *dic1 = responseObject;
        NSMutableDictionary *focDic = [dic1 objectForKey:@"focusImages"];
        NSMutableArray *listArray = [focDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in listArray) {
            Raido1 *raido1 = [[Raido1 alloc] init];
            [raido1 setValuesForKeysWithDictionary:dic];
            [self.dataArray1 addObject:raido1];
            [raido1 release];
//            NSLog(@"%ld",(long)raido1.trackId);
        }
        NSMutableDictionary *cateDic = [dic1 objectForKey:@"categories"];
        NSMutableArray *dataArray = [cateDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in dataArray) {
            Raido1Button *raiduButton = [[Raido1Button alloc] init];
            [raiduButton setValuesForKeysWithDictionary:dic];
            if ([raiduButton.title isEqualToString:@"音乐"]) {                            [self.dataArray2 addObject:raiduButton];
            }
            if ([raiduButton.title isEqualToString:@"最新资讯"]) {
                [self.dataArray2 addObject:raiduButton];
            }
            if ([raiduButton.title isEqualToString:@"情感生活"]) {
                [self.dataArray2 addObject:raiduButton];
            }
            if ([raiduButton.title isEqualToString:@"电台"]) {                            [self.dataArray2 addObject:raiduButton];
            }
            [raiduButton release];
        }
        self.myScrollView.array = self.dataArray1;
        self.myScrollView.arrayButton = self.dataArray2;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"第一部分数据获取失败");
    }];
    // 获取第二部分数据
    self.dataDic2 = [NSMutableDictionary dictionary];
    self.dataArray2DT = [NSMutableArray array];
    self.dataArray2QG = [NSMutableArray array];
    self.dataArray2YY = [NSMutableArray array];
    self.dataArray2ZX = [NSMutableArray array];
    AFNetworkReachabilityManager *netWorkManager2 = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string2 = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/index_hotsounds?device=iPhone"];
    AFHTTPRequestOperationManager* manager2 = [AFHTTPRequestOperationManager manager];
    manager2.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager2 GET:url_string2 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager2 stopMonitoring];
        
        // 刷新删除原数据
        [self.dataDic2 removeAllObjects];
        
        NSMutableDictionary *dic2 = responseObject;
        NSMutableArray *array = [dic2 objectForKey:@"categories"];
        for (NSMutableDictionary *dic in array) {
            NSString *title = [dic objectForKey:@"title"];
            NSMutableArray *array1 = [dic objectForKey:@"sounds"];
            for (NSMutableDictionary *dic in array1) {
                Raido2 *raido2 = [[Raido2 alloc] init];
                [raido2 setValuesForKeysWithDictionary:dic];
                if ([title isEqualToString:@"最新资讯"]) {
                    [self.dataArray2ZX addObject:raido2];
                }
                if ([title isEqualToString:@"音乐"]) {
                    [self.dataArray2YY addObject:raido2];
                }
                if ([title isEqualToString:@"情感生活"]) {
                    [self.dataArray2QG addObject:raido2];
                }
                if ([title isEqualToString:@"电台"]) {
                    [self.dataArray2DT addObject:raido2];
                }
                [self.dataDic2 setValue:self.dataArray2DT forKey:@"DT"];
                [self.dataDic2 setValue:self.dataArray2QG forKey:@"QG"];
                [self.dataDic2 setValue:self.dataArray2YY forKey:@"YY"];
                [self.dataDic2 setValue:self.dataArray2ZX forKey:@"ZX"];
                [raido2 release];
            }
        }
        self.myScrollView.dictionary = self.dataDic2;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"第二部分数据获取失败");
    }];

}

// 实现button协议方法
- (void)pushToConllectionViewController:(NSInteger)tag
{
     MyCollectionViewController *collection = [[MyCollectionViewController alloc] init];
    
    // 确定点击获得的接口是哪个, 获得数据
    // 用于确定点击的button是哪一个, 从而知道获得那个接口数据(顺序为:1.最新资讯, 2.电台, 3.音乐, 4.情感生活)
    if (tag == 1 || tag == 5) {
        collection.type = 1;
    }
    if (tag == 2 || tag == 6) {
        collection.type = 2;
    }
    if (tag == 3 || tag == 7) {
        collection.type = 3;
    }
    if (tag == 4 || tag == 8) {
        collection.type = 4;
    }
    [self.navigationController pushViewController:collection animated:YES];
}
// 实现imageview协议方法
- (void)imageViewTap:(NSInteger)tag
{
    [[PlayerViewController sharePlayerDataHandel] streamerStop];
    // 把点击的imageview对应的raido1ImageView对象传入播放器
    Raido1 *raido1 = [self.dataArray1 objectAtIndex:tag - 1];

    
    NSString *string1 = @"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=";
    NSString *string2 = [NSString stringWithFormat:@"%d", raido1.trackId];
    NSString *url_string = [string1 stringByAppendingString:string2];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSMutableDictionary *dictionary = responseObject;
        Raido1ImageView *raido1ImageView = [[Raido1ImageView alloc] init];
        [raido1ImageView setValuesForKeysWithDictionary:dictionary];
        
        [PlayerViewController sharePlayerDataHandel].raido1ImageView = raido1ImageView;
        [[PlayerViewController sharePlayerDataHandel] buttonPressed];
        
        [raido1ImageView release];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败==== %@ , urlrequest = %@",error,operation.request);
    }];
    
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:[PlayerViewController sharePlayerDataHandel]];
    [PlayerViewController sharePlayerDataHandel].navigationController.navigationBarHidden = YES;
    [self presentViewController:naVC animated:YES completion:^{
        
        
    }];
    [naVC release];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
